package com.guyu.common.core.entity.page;

import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * API分页请求参数
 */
@Getter
@NoArgsConstructor
public class PageRequest {

    /**
     * 页码，从1开始
     */
    private long pageNo = 1;
    /**
     * 每页显示数量[1,1000]
     */
    private long pageSize = 10;


    public PageRequest(long pageNo, long pageSize) {
        if (pageNo < 1) {
            throw new IllegalArgumentException("Page index must not be less than 1!");
        }
        if (pageSize < 1) {
            throw new IllegalArgumentException("Page size must not be less than 1!");
        }
        if (pageSize > 1000) {
            throw new IllegalArgumentException("Page size must not be greater than 1000!");
        }

        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    public static PageRequest of(long pageNo, long pageSize) {
        return new PageRequest(pageNo, pageSize);
    }


    public void setPageSize(long pageSize) {
        if (pageSize < 1) {
            throw new IllegalArgumentException("Page size must not be less than 1!");
        }
        if (pageSize > 1000) {
            throw new IllegalArgumentException("Page size must not be greater than 1000!");
        }
        this.pageSize = pageSize;
    }

    public void setPageNo(long pageNo) {
        if (pageNo < 1) {
            throw new IllegalArgumentException("Page index must not be less than 1!");
        }
        this.pageNo = pageNo;
    }

    /**
     * 起始位置，偏移位置
     */
    public long getOffset() {
        return (pageNo - 1) * pageSize;
    }

    /**
     * 校验分页深度
     */
    public void checkDepth() {
        if (pageNo * pageSize > 5000) {
            throw new IllegalArgumentException("Page Depth must not be greater than 5000!");
        }
    }

}
